﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VTV.TaskManagementSystem.Entities;

namespace VTV.TaskManagementSystem.Common
{
    public class RenderHTML_Ver2
    {
        public static string ShowMeVer2s(IEnumerable<ShowMeVer2> showMeVer2s)
        {
            try
            {
                var result = "";
                showMeVer2s.ToList().ForEach(me => result += ShowMeVer2(me));

                return result;
            }
            catch (Exception e)
            {
                return null;
            }
        }

        private static string ShowMeVer2(ShowMeVer2 showMeVer2)
        {
            string result = "<div class='v2-row-wrapper'>" +
                                "<div class='v2-row-item {a}'>" +
                                    "<div class='v2-item1 {status-text}'>" + showMeVer2.TaskName + "</div>" +
                                    "<div class='v2-item2'>" + showMeVer2.TeamName + "</div>" +
                                    "<div class='v2-item3 {status-text}'>{status-value}</div>" +
                                    "<div class='v2-item4'>" + (showMeVer2.StartDate.HasValue ? showMeVer2.StartDate.Value.ToString("dd/MM/yyyy") : string.Empty) + "</div>" +
                                    "<div class='v2-item5'>" + (showMeVer2.ExpectedEndDate.HasValue ? showMeVer2.ExpectedEndDate.Value.ToString("dd/MM/yyyy") : string.Empty) + "</div>" +
                                    "<div class='v2-item6'>" + showMeVer2.Comments + "</div>" +
                                    "<div class='v2-item7 {status}'>" +
                                        "<div class='v2-item7-left'>" +
                                            "<span style='margin-right: 2px; float: left;'>Thành viên:</span>" +
                                            "<ol style='margin-left: 35px; display: block; float: left;'>{emp}</ol>" +
                                        "</div>" +
                                        "<div class='v2-item7-right'>" +
                                            "<span style='margin-right: 5px; clear: both;'>Mô tả công việc:</span><p style='margin-left: 2px;'>" +
                                            showMeVer2.TaskDescription + "</p>" +
                                        "</div>" +
                                     "</div>" +
                                 "</div>" +
                             "</div>";

            switch (showMeVer2.Status)
            {
                case 'D':
                case 'L':
                    result = result.Replace("{status-value}", "Đã hoàn thành");
                    result = result.Replace("{status}", "");
                    result = result.Replace("{status-text}", "");
                    result = result.Replace("{a}", "");
                    break;
                case 'I':
                    result = result.Replace("{status-value}", "Đang thực hiện");
                    if(showMeVer2.ExpectedEndDate != null && (DateTime.Today == showMeVer2.ExpectedEndDate || DateTime.Today == showMeVer2.ExpectedEndDate.Value.AddDays(-1)))
                    {
                        result = result.Replace("{status}", "v2-row-item-deadline");
                        result = result.Replace("{status-text}", "v2-status-deadline");
                        result = result.Replace("{a}", "v2-row-item-shadow-deadline");
                    }
                    else if (showMeVer2.ExpectedEndDate != null && DateTime.Today > showMeVer2.ExpectedEndDate)
                    {
                        result = result.Replace("{status}", "v2-row-item-late");
                        result = result.Replace("{status-text}", "v2-status-late");
                        result = result.Replace("{a}", "v2-row-item-shadow-late");
                    }
                    break;
                default:
                    result = result.Replace("{status-value}", string.Empty);
                    break;
            }

            if (showMeVer2.EmployeeToShows != null && showMeVer2.EmployeeToShows.Count > 0 && showMeVer2.EmployeeToShows.First().EmployeeID.HasValue)
            {
                var emp = showMeVer2.EmployeeToShows.Aggregate("", (current, employeeToShow) => current + EmployeeToShow(employeeToShow));
                result = result.Replace("{emp}", emp);
            }
            else
            {
                result = result.Replace("{emp}", "Không có thành viên nào");
            }

            return result;
        }

        private static string EmployeeToShow(EmployeeToShow employeeToShow)
        {
            string result;

            var isTeamLead = employeeToShow.IsTeamLead.HasValue && employeeToShow.IsTeamLead.Value ? "class='team-member-name-leader'" : string.Empty;
            result = "<li " + isTeamLead + ">" + employeeToShow.EmployeeName + "</li>";

            return result;
        }
    }
}
